<!DOCTYPE html>
<html lang="en">

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title>华胜后台管理</title>
		<link rel="stylesheet" type="text/css" href="css/unified.css">
		<script src="plugins/layui/layui.js"></script>

		<style type="text/css">

		</style>
	</head>

	<body>

		<fieldset class="layui-elem-field layui-field-title">
			<legend>商品库存信息</legend>
			<div class="layui-field-box">
				<table class="layui-table " lay-filter="table100" id="stockinTable"></table>
				<table class="layui-table smtable"  id="hiddentable">
					<thead>
						<tr>
							<td style="width: 28px;">操作</td>
							<td>状态</td>
							<td>条码</td>
							<td>商品编码</td>
							<td>名称</td>
							<td>产品图片</td>
							<td>商家编号</td>
							<td>库存地点</td>
							<td>库存数量</td>
							<td>出库数量</td>
						</tr>
					</thead>

				</table>
				<div class="div_tablefoot">
					<div class="TotalTr">
						<div class="layui-col-md12">
							<div><label>已付金额/元:</label><input type="number" id="realIncome" name="realIncome" placeholder="" class="editinput min_140" value="0"/></div>
						</div>
						<div class="layui-col-md12">
							<div><label>应付金额/元:</label><b class="min_140" id="paymentAmount" name="paymentAmount" >0.00</b></div>
						</div>
					</div>
					<div id="addtr" class="TapAdd">点击添加+</div>

				</div>

				<!--<table class="layui-table smtable"  id="stockinTable">
					<thead>
						<tr>
							<td style="width: 28px;">操作</td>
							<td>状态</td>
							<td>条码</td>
							<td>商品编码</td>
							<td>名称</td>
							<td>产品图片</td>
							<td>商家编号</td>
							<td>库存地点</td>
							<td>库存数量</td>
							<td>单价</td>
							<td>金额</td>
							<td>折扣</td>
							<td>折后金额</td>
							<td>出库数量</td>
						</tr>
					</thead>
					<tbody>
						
					</tbody>
					<tfoot>
						
						<tr><td colspan='100%' ></td></tr>
						<tr>
							<td colspan='6' ></td>
							<td >已收金额/元</td>
							<td ><input class="editTinput" type="number" name="realIncome" value="0"  min="0"/ ></td>
							<td >应收金额/元</td>
							<td >100.00</td>
						</tr>
						<tr>
							<td colspan='100%' id="addtr"><b style="color: #48a08e;    cursor: pointer;">点击添加+</b></td>
						</tr>
					</tfoot>
				</table>-->
			</div>
		</fieldset>

		<fieldset class="layui-elem-field layui-field-title">
			<legend>销售订单</legend>
			<div class="layui-field-box">
				<form class="layui-form layui-form-pane IntTable" action="">

					<div class="layui-col-xs12 layui-col-sm6 layui-col-md3">
						<div class="layui-form-item">
							<label class="layui-form-label">客户</label>
							<div class="layui-input-block">
								<input type="button" id="userSelect" class="layui-btn formTbtn" value="点击选择客户">

							</div>
						</div>
					</div>
					<input type="text" value="" hidden="hidden" class="inputRequired userId" name="userId">
					<input type="text" value="" hidden="hidden" class="inputRequired contactName" name="contactName">
					<input type="text" value="" hidden="hidden" class="inputRequired contactPhone" name="contactPhone">
					<input type="text" value="" hidden="hidden" class="inputRequired orderAddress" name="orderAddress">
					<input type="text" value="" hidden="hidden" class="inputRequired undertookId" name="undertookId">

					<div class="layui-col-xs12 layui-col-sm6 layui-col-md3">

						<div class="layui-form-item">
							<label class="layui-form-label">经手人</label>
							<div class="layui-input-block">
								<input type="button" id="salesSelect" class="layui-btn formTbtn" value="点击选择经手人">

							</div>
						</div>
					</div>

					<div class="layui-col-xs12 layui-col-sm6 layui-col-md3">
						<div class="layui-form-item">
							<label class="layui-form-label">配送方式</label>
							<div class="layui-input-block">
								<input type="text" name="shippingMethod" autocomplete="off" placeholder="" class="layui-input inputRequired" value="自提">
							</div>
						</div>
					</div>
					<!--					
					付款状态 Unfinished 未完成 PendingPay待付款 Abnormal异常 Success 成功 

Complete 订单完成 PartialPay 部分支付-->

					<!--<div class="layui-col-xs12 layui-col-sm6 layui-col-md3">
						<div class="layui-form-item">
						    <label class="layui-form-label">付款状态</label>
						    <div class="layui-input-block">
						    	 <select name="paymentMethod">
							       
							        <option value="PendingPay">待付款</option>
							        <option value="Success">付款完成</option>
							        <option value="Unfinished">未完成</option>
							        <option value="Unfinished">未完成</option>
							        <option value="Unfinished">未完成</option>
							      </select>
						    	
						    
						    </div>
						 </div>
					</div>-->
					<div class="layui-col-xs12 layui-col-sm6 layui-col-md3">
						<div class="layui-form-item">
							<label class="layui-form-label">订单地区编号</label>
							<div class="layui-input-block">
								<input type="text" name="orderAdcode" autocomplete="off" placeholder="" class="layui-input inputRequired" value="10000">
							</div>
						</div>
					</div>

					<!--<div class="layui-col-xs12 layui-col-sm6 layui-col-md3">
						<div class="layui-form-item">
						    <label class="layui-form-label">应付金额</label>
						    <div class="layui-input-block">
						      <input type="text" name="paymentAmount" autocomplete="off" placeholder="" class="layui-input inputRequired" value="100">
						    </div>
						 </div>
					</div>
					
					<div class="layui-col-xs12 layui-col-sm6 layui-col-md3">
						<div class="layui-form-item">
						    <label class="layui-form-label">实收金额</label>
						    <div class="layui-input-block">
						      <input type="text" name="realIncome" autocomplete="off" placeholder="" class="layui-input inputRequired" value="0">
						    </div>
						 </div>
					</div>-->

				</form>

			</div>
		</fieldset>
		<div class="layui-layer-btn layui-layer-btn-" style="margin-bottom: 35px;">
		
			<div class="layui-btn yesInsert openwinbtn " data-method="Yesmang">确认生成订单</div>
			<!--<div class="layui-btn layui-bg-gray noInsert">取消</div>-->
		</div>

		<script id="ComManagement" type="text/html">
			<table class="layui-table " lay-filter="table100" id="cMtable"></table>
		</script>
	</body>

	<script src="js/jquery-3.2.1.min.js"></script>
	<script src="js/allajax.js"></script>
	<script>
		//	$(".stockinTable")
		var offlineJson = new Array();
		layui.use(['layer', 'form', 'element', 'table', 'laydate'], function() {
			var layer = layui.layer,
				form = layui.form,
				element = layui.element,
				table = layui.table,
				laydate = layui.laydate;
				
				
//				laypage = layui.laypage;
//			productListFn("[]")

			
			AjaxPostFn('system/bizmerchantinfo/select?page=1&pagesize=100', '{}', function(data) {
				//获取商家
				if(data.pageInfo.size != 0) {
					$.each(data.pageInfo.list, function(idx, obj) {
						$("#merchantNumselect").append("<option value='" + obj.merchantNumber + "'>" + obj.merchantName + "</option>");
					});
					form.render();

				} else {

				}
			})

			$("#addtr").click(function() {

				var index = layer.open({
					type: 2,
					title: "商品库存信息",
					area: ['100%', '100%'],
					maxmin: true,
					shade: 0,
					content: 'stock_now.html?urltype=offline',
					end :function(){
						$("#hiddentable").remove()
//						table.reload('ORDERinTable',{
//						  	where: { //设定异步数据接口的额外参数，任意设
//							    stats: '<b data-stats="0">等待出库</b>'
//							   
//							}}
//						);
						//bug 渲染重载只能重载一次,问题暂时不知是什么.此处先复制一遍重新加载 2018-05-22
						tableIns = table.render({
							elem: '#stockinTable',
							cols: [
								[ //标题栏
									{
										field: 'editStats',
										minWidth: '0%',
										width: '0%',
										type: 'space',
										style: 'display:none'
									},{
										field: 'operation',
										title: '操作',
										sort: true
									}, {
										field: 'stats',
										title: '状态',
										sort: true
									}, {
										field: 'barCode',
										title: '产品条码'
									}, {
										field: 'commodityNumber',
										title: '商品编码'
									}, {
										field: 'commodityName',
										title: '产品名称'
									}, {
										field: 'homePhoto1',
										title: '主图'
									}, {
										field: 'merchantNumber',
										title: '商家编号'
									}, {
										field: 'locationId',
										title: '库存地点'
									}, {
										field: 'theNum',
										title: '库存数量'
									}, {
										field: 'buyAmount',
										title: '出库数量' + modDot,
										edit: 'text'
									}, {
										field: 'Total0',
										title: '单价' + modDot,
										edit: 'text'
										
									}, {
										field: 'discount',
										title: '折扣' + modDot,
										edit: 'text'
									}, {
										field: 'Total1',
										title: '折后金额' + modDot,
										edit: 'text'
									}
								]
							],
							data: offlineJson,
							id: 'ORDERinTable',
							size: 'lg',
							//						height: 'full-250',
		//					even: true,
		//					page: true, //是否显示分页
							done: function() {
								console.log(offlineJson)
								//图片点击放大
								TopimgFn();
								totolFn();
								
							}
						});
					}
				});

			})
			
			
			function totolFn(){
				//处理总计问题
				$("#paymentAmount").html('0.00');
				$.each(offlineJson, function(idx , obj) {
					var thisTrjson = offlineJson[idx];
					
						$.getJSON(AllUrl+"api/bizcommodity/selectbynumber?adcode=100000&commoditynumber="+obj.commodityNumber, function(json){
							if(offlineJson[idx].editStats == 0){
								
								
								thisTrjson.Total0 = ( json.dataResultObj.presentPrice / 100 ) ; //单价的获取
								
						  	}
							
							thisTrjson.Total1 = thisTrjson.discount * thisTrjson.Total0 * thisTrjson.buyAmount;
							offlineJson = table.cache.ORDERinTable;
							var NewrealIncome = parseFloat($("#paymentAmount").html()) +  thisTrjson.Total1;
								$("#paymentAmount").html(NewrealIncome);
							  	productListFn();
						  	
						});
					
					
				});
			}
			
			
			
			table.on('edit(table100)', function(obj) {
				var value = obj.value //得到修改后的值
					,data = obj.data //得到所在行所有键值
					,field = obj.field; //得到字段
					
					data.Total1 =  data.discount * data.Total0 * data.buyAmount;
					data.editStats =  1 //已经修改过了
					offlineJson = table.cache.ORDERinTable;
					totolFn();

			})
			
			

			$("#userSelect").click(function() {
				
				var index = layer.open({
					type: 2,
					title: "客户选择",
					area: ['100%', '100%'],
					maxmin: true,
					shade: 0,
					content: 'offline_realUser.html?urltype=choice'
				});

			})
			$("#salesSelect").click(function() {

				var index = layer.open({
					type: 2,
					title: "客服选择",
					area: ['100%', '100%'],
					maxmin: true,
					shade: 0,
					content: 'offline_saleman.html?urltype=choice'
				});

			})

			$(".yesInsert").click(function() {
				
				var tablejson = table.cache.ORDERinTable;//table的data数据
				var commodity = new Array();//新建一个放置处理之后的商品数据
				console.log(tablejson)
				if(tablejson.length != 0){
//					判断库存信息是否已经选择

					inputisNull('.inputRequired', function() {
//						var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
						var injson = $(".IntTable").serializeJson();
						injson.generateTime = new Date().getTime();
						injson.paymentMethod = "Reality"; //订单类型
						injson.paymentAmount = $("#paymentAmount").html();
						injson.realIncome = $("#realIncome").val();
						console.log(injson)
						var thisjson = '{"orderinfo":' + JSON.stringify(injson) + ',"commodity":' + JSON.stringify(table.cache.ORDERinTable) + ',"incidental":[]}';
						AjaxPostFn('system/orderinfo/generaterealityorder', thisjson, function(data) {
							if(data.htmlState.status == 200) {
								layer.closeAll('iframe');
								layer.msg("生成订单成功,请尽快出库货物!")

							}
						})
					})
					
				} else {
					layer.msg("请选择至少一件商品")
				}

			})

		})

		function deltr(objidx) {

			layer.confirm('是否要删除当前选中行信息', function(index) {
				//do something
				$("#stockinTable tbody tr:eq(" + objidx + ")").remove();
				layer.close(index);
			});

		}
	</script>

</html>